package com.ewzj.bigdata.mvp.ui.adapter

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.ewzj.bigdata.R

import com.ewzj.bigdata.mvp.model.rspbean.RspOriginBean
import com.jess.arms.utils.UiUtils
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration

import butterknife.BindView
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.extensions.dpToPx
import com.ewzj.bigdata.app.extensions.loadImgwithloading
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.mvp.model.rspbean.InfoBean
import com.ewzj.bigdata.mvp.ui.activity.InformercialActivity
import com.ewzj.bigdata.mvp.ui.activity.NewsWebActivity

/**
 * author:zhangjing
 * 作用:
 * return:
 */

class OriginAdapter(data: List<RspOriginBean>) : BaseMultiItemQuickAdapter<RspOriginBean, BaseViewHolder>(data) {
    /*
    * 0为横向recylerview类型
    * 1 图右字左 2大图
    *
    * */
    init {
        addItemType(0, R.layout.item_origin_type3)
        addItemType(1, R.layout.item_origin_type1)
        addItemType(2, R.layout.item_origin_type2)
        addItemType(3, R.layout.item_origin_type4)
    }

    override fun convert(baseViewHolder: BaseViewHolder, rspOriginBean: RspOriginBean) {
        baseViewHolder.getConvertView().setOnClickListener {
//            if ("xiangmu".equals()rspOriginBean.news_type.equals()){
//                var intent= Intent(mContext,InformercialActivity::class.java)
//                intent.putExtra(AppKey.EXTRA_String,rspOriginBean.project_name)
//                mContext.startActivity(intent)
//                return@setOnClickListener
//            }
            val infoBean = InfoBean()
            infoBean.setGo_url(rspOriginBean.go_url)
            infoBean.setTitle(rspOriginBean.title)
            infoBean.setSources(rspOriginBean.sources)
            infoBean.setId(rspOriginBean.id)
            infoBean.thumb=rspOriginBean.thumb
            infoBean.setGroupId(rspOriginBean.groupId)
            infoBean.setIs_self(rspOriginBean.is_self)
            infoBean.setIs_collect(rspOriginBean.is_collect)
            infoBean.setTopic_sign(rspOriginBean.topic_sign)
            infoBean.summary=rspOriginBean.summary
            AppKey.mchaneObject=rspOriginBean
            NewsWebActivity.startInfoActivity(mContext, infoBean)
        }
        when (baseViewHolder.itemViewType) {
            0 -> if (rspOriginBean.topic != null) {
                val recyclerView = baseViewHolder.getView<RecyclerView>(R.id.rv_origin)
                if (recyclerView.adapter == null) {
                    recyclerView.addItemDecoration(VerticalDividerItemDecoration.Builder(mContext).size(10.dpToPx(mContext))
                            .colorResId(R.color.white).build())
                }
                val originInnerAdapter = OriginInnerAdapter(rspOriginBean.topic)
                val linearLayoutManager = LinearLayoutManager(mContext)
                linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
                recyclerView.layoutManager = linearLayoutManager
                recyclerView.adapter = originInnerAdapter
            }
            1 -> {
                baseViewHolder.getView<View>(R.id.iv_pic) as ImageView
                baseViewHolder.getView<ImageView>(R.id.iv_pic).loadImgwithloading(rspOriginBean.thumb);
                settag(rspOriginBean, baseViewHolder.getView<View>(R.id.tv_title) as TextView)
                baseViewHolder.setText(R.id.tv_source, rspOriginBean.sources)
                baseViewHolder.setText(R.id.tv_time, rspOriginBean.create_time)
                baseViewHolder.setText(R.id.tv_watch, rspOriginBean.visit)
                baseViewHolder.setText(R.id.tv_message, rspOriginBean.comment)
            }
            2 -> {
                baseViewHolder.getView<ImageView>(R.id.iv_pic).loadImgwithloading(rspOriginBean.thumb);
                settag(rspOriginBean, baseViewHolder.getView<View>(R.id.tv_title) as TextView)
                baseViewHolder.setText(R.id.tv_source, rspOriginBean.sources)
                baseViewHolder.setText(R.id.tv_time, rspOriginBean.create_time)
                baseViewHolder.setText(R.id.tv_watch, rspOriginBean.visit)
                baseViewHolder.setText(R.id.tv_message, rspOriginBean.comment)
            }
            3->{
                settag(rspOriginBean, baseViewHolder.getView<View>(R.id.tv_title) as TextView)
                baseViewHolder.setText(R.id.tv_source, rspOriginBean.sources)
                baseViewHolder.setText(R.id.tv_time, rspOriginBean.create_time)
                baseViewHolder.setText(R.id.tv_watch, rspOriginBean.visit)
                baseViewHolder.setText(R.id.tv_message, rspOriginBean.comment)
            }
        }
    }

    internal fun settag(infoBean: RspOriginBean, textView: TextView) {
        if ("xiangmu".equals(infoBean.news_type)){
            Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.xiangmu)
        }else{
            if (TextUtils.isEmpty(infoBean.tags)) {
                textView.text = infoBean.title
                return
            } else {
                when (infoBean.tags) {
                    "ads" -> Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.ad_info)
                    "hot" -> Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.home_infhot)
                    "vip" -> Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.home_infzhuan)
                    "new" -> Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.icon_new)
                    "topic" -> Apputils.Utils.lefttopTextview(textView, infoBean.title, R.drawable.xiangmu)
                    else-> textView.text = infoBean.title
                }
            }
        }

    }
}
